package com.syni.mdd.sjb.common.datatreaure.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DxshAppNewsPush;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.mapper.DxshAppNewsPushMapper;
import com.syni.mdd.sjb.common.component.utils.common.utils.DateTimeUtils;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.sjb.common.datatreaure.service.DxshAppNewsPushService;
import com.syni.mdd.sjb.common.datatreaure.constants.ResultMsg;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.dxshAppNews.SettingStatusVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lcc
 * @since 2020-09-25
 */
@Service
public class DxshAppNewsPushServiceImpl extends ServiceImpl<DxshAppNewsPushMapper, DxshAppNewsPush> implements DxshAppNewsPushService {

    @Autowired
    DxshAppNewsPushMapper dxshAppNewsPushMapper;

    @Override
    public DxshAppNewsPush findByUserId(Integer userId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DxshAppNewsPush.USER_ID,userId);
        return dxshAppNewsPushMapper.selectOne(queryWrapper);
    }

    @Override
    public Map<String, Object> editPushTime(SettingStatusVo settingStatusVo) {
        DxshAppNewsPush dxshAppNewsPush = findByUserId(settingStatusVo.getUserId());
        if(dxshAppNewsPush == null){
            dxshAppNewsPush = new DxshAppNewsPush();
        }
        BeanUtils.copyProperties(settingStatusVo,dxshAppNewsPush);
        dxshAppNewsPush.setNewTime(LocalDateTime.now());
        this.saveOrUpdate(dxshAppNewsPush);
        return ResultMapHelper.success(null, ResultMsg.HAND_SUCCESS);
    }

    @Override
    public void addPushTime(String pushTime,Integer userId) {
        DxshAppNewsPush dxshAppNewsPush = new DxshAppNewsPush();
        dxshAppNewsPush.setUserId(userId);
        dxshAppNewsPush.setNewTime(LocalDateTime.now());
        dxshAppNewsPush.setPushTime(pushTime);
        this.save(dxshAppNewsPush);
    }

    @Override
    public boolean checkHavePush(Integer userId) {
        Date nowTime = new Date();
        DxshAppNewsPush dxshAppNewsPush = findByUserId(userId);
        String pushTime = PUSH_TIME;
        if(dxshAppNewsPush != null){
            pushTime = dxshAppNewsPush.getPushTime();
        }
        String[] startEnd = pushTime.split("~");
        Long startTime = DateTimeUtils.converStringToDate(String.format("%s %s",DateTimeUtils.converDateToString(nowTime,DateTimeUtils.DATE_PATTERN_DAY) + " " + startEnd[0],DateTimeUtils.DATE_PATTERN_TIMESTAMP_02),DateTimeUtils.DATE_PATTERN_TIMESTAMP_02).getTime();
        Long endTime = DateTimeUtils.converStringToDate(String.format("%s %s",DateTimeUtils.converDateToString(nowTime,DateTimeUtils.DATE_PATTERN_DAY) + " " + startEnd[1],DateTimeUtils.DATE_PATTERN_TIMESTAMP_02),DateTimeUtils.DATE_PATTERN_TIMESTAMP_02).getTime();
        return nowTime.getTime() >= startTime && nowTime.getTime() <= endTime;
    }
}
